{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sympy as sy\n",
    "sy.init_printing()  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"red\"> Similarity </font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If $A = PBP^{-1}$, we say $A$ is <font face=\"gotham\" color=\"red\">similar</font> to $B$, decomposing $A$ into $PBP^{-1}$ is also called a <font face=\"gotham\" color=\"red\">similarity transformation</font>.\n",
    "\n",
    "<font face=\"gotham\" color=\"red\">If $n\\times n$ matrices $A$ and $B$ are similar, they have the same eigenvalues</font>.\n",
    "\n",
    "The <font face=\"gotham\" color=\"red\">diagnoalization</font>, which we will explain,  is a process of finding similar matrices."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Diagonalizable Matrix</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let $A$ be an $n\\times n$ matrix. If there exists an $n\\times n$ invertible matrix $P$ and a diagonal matrix $D$, such that\n",
    "\n",
    "$$\n",
    "A=PDP^{-1}\n",
    "$$\n",
    "\n",
    "then matrix $A$ is called a diagonalizable matrix."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And futher, <font face=\"gotham\" color=\"red\">the columns of $P$ are linearly independent eigenvectors of $A$, and its corresponding eigenvalues are on the diagonal of $D$. In other words, $A$ is diagonalizable if and only if the dimension of eigenspace basis is $n$</font>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's easy to show why this equation holds."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\n",
    "\n",
    "$$\n",
    "P = \\left[\\begin{array}{llll}\n",
    "{v}_{1} & {v}_{2} & \\cdots & {v}_{n}\n",
    "\\end{array}\\right]\\\\\n",
    "$$\n",
    "\n",
    "$$\n",
    "D = \\left[\\begin{array}{cccc}\n",
    "\\lambda_{1} & 0 & \\cdots & 0 \\\\\n",
    "0 & \\lambda_{2} & \\cdots & 0 \\\\\n",
    "\\vdots & \\vdots & & \\vdots \\\\\n",
    "0 & 0 & \\cdots & \\lambda_{n}\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "\n",
    "where $v_i, i \\in (1, 2, ...n)$ is an eigenvector of $A$, $\\lambda_i, i \\in (1, 2, ...n)$ is an eigenvalue of $A$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "AP = A\\left[\\begin{array}{llll}\n",
    "{v}_{1} & {v}_{2} & \\cdots & {v}_{n}\n",
    "\\end{array}\\right]=\\left[\\begin{array}{llll}\n",
    "A {v}_{1} & A {v}_{2} & \\cdots & A {v}_{n}\n",
    "\\end{array}\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$P D=P\\left[\\begin{array}{cccc}\n",
    "\\lambda_{1} & 0 & \\cdots & 0 \\\\\n",
    "0 & \\lambda_{2} & \\cdots & 0 \\\\\n",
    "\\vdots & \\vdots & & \\vdots \\\\\n",
    "0 & 0 & \\cdots & \\lambda_{n}\n",
    "\\end{array}\\right]=\\left[\\begin{array}{lllll}\n",
    "\\lambda_{1} {v}_{1} & \\lambda_{2} {v}_{2} & \\cdots & \\lambda_{n} {v}_{n}\n",
    "\\end{array}\\right]$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We know that $A{v}_i = \\lambda_i{v}_i$, i.e.\n",
    "\n",
    "$$\n",
    "AP = PD\n",
    "$$\n",
    "\n",
    "Since $P$ has all independent eigenvectors, then\n",
    "\n",
    "$$\n",
    "A = PDP^{-1}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Diagonalizing a Matrix</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider a matrix\n",
    "\n",
    "$$A=\\left[\\begin{array}{rrr}\n",
    "1 & 3 & 3 \\\\\n",
    "-3 & -5 & -3 \\\\\n",
    "3 & 3 & 1\n",
    "\\end{array}\\right]$$\n",
    "\n",
    "diagonalize the matrix $A$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Following these steps:\n",
    "\n",
    "1. Find the eigenvalues of $A$\n",
    "2. Find the eigenvectors of $A$\n",
    "3. Construct $P$.\n",
    "4. Construct $D$ from the corresponding columns of $P$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdQAAABLCAYAAADJYHZOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAQ2UlEQVR4Ae1dba7VRhIlI36PnhJpFvDYwSOsILADICtIsgMQK4hgB5AVZGAHIStgeDuABYw0BM3vkZhzLl0X42e/a3dXf9mnJV/bbXdX1anqqu522/fGjRs3LrB9mtlefvr06UbKhnrfYrubUsfeygKvKJ3ElvPGF3y8wzZpU960YurrHadYfFHuPJQ9i8Gt1TKQaXc+BjLv0kcE+13tW1DOxfZP0b+JGyw9wwEd4TC9H56sPf7mm2+eo8x7NMTXa8vq/gMCsTqJLecF+9OJiu4h7/5Efs2sXnGKwhft8D3a5CsA/hu2BzWB96ItH3Mj1oZjy3mpLsqGQTyqnKPtn6RvPZ1zzx4oBKfz/AvbpnrDnhjN1QXMonQSW26OD8988PYzNlSZNuPhUX6LOK3BF/eyXf7sgWXNOiDDbn0MZJePCL6EtowNpnjat+A+d9sf0v8bTtwTeo1nqJS94McQ8qM7AVXYBALQ8wW2d0HfTfDUIhMN4sTR6XPwxWmwLlOwOfmYytpr0LZPIZLV9odTvqcYWXOdhs6p3hdrCune9hEYOLIP4PZ7bN065Zxot4wT2uVr8MepXz6S4VR8j0k+ppLWWrbtU5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ofs0KbunUNAq627xJQA/e/Ys9pX4v4a4TSve0gwJHA1Ic6+DWsy8Eoth2OxUk0AmHKi49qXkRXUq6gfEw5rDdPKYftuwVUONpnQQP8coxSpwhAj3SsH2Bs/KTkIeGY070Psf30OUe/G0LgSZCFwarpJB/TtHp6ZM7d9m86o8Cgyo+p89N19jzOmYSqK4AAR6NPoEeb+r2D8x+g06kvKBVgp00SwIejeXY2bETP7x0TozcD598m8+CK7RQ7dpxeddRe5WMasCjZ/rQSvAMqe7mPsDHyP54mqdzWEQjOVfo7oSjgxM809pxszUNPupaPacDiZPvTSnCb8mX1A0d8GKVOk1SuEBACjSDAju8LtNtuVm7LxzRiOf2zkcX2XQMqMYbBc0qG0702P81sJSEgBBpCAFN2nEnilG9Po9MDgvIxDRlSh6zktH33gBrw5eIVjlLtGVyHsItlIbBNBNAuGUifYvsljPh6FFQ+pketVeY5t+1nCahopHyfjdvzyviJvBAQAlcR4B+jv0Y77eFVmavcI0c+ZhIWZZ5GIKvtZwmolAkGzwUb5+gR2MKH06LqDiEgBLIigPbI98S59b6gSj4mq6Vsr/JSts+Xuj/NbC8RGGm4URvqZN1/YeNrNFF17LFcwG21TmLLeWMMPt5hm+Tfm1ZMfb3jlIJvKHs3BrcWywRd7s7HxNpwbDlv3Qc7XO0jYsuR/1A2yfZP0edrM1zhN/cxhqTVfxDiEr0C9oT52TouVFJahkCsTmLLLeNq+V18Ptdy6h2nKHzRFvnslM9NN/P5yB37mFgbji3n3Z6jbBhMRJVztP1r6f8fJZlseN++juMAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left[ \\left( -2, \\  2, \\  \\left[ \\left[\\begin{matrix}-1\\\\1\\\\0\\end{matrix}\\right], \\  \\left[\\begin{matrix}-1\\\\0\\\\1\\end{matrix}\\right]\\right]\\right), \\  \\left( 1, \\  1, \\  \\left[ \\left[\\begin{matrix}1\\\\-1\\\\1\\end{matrix}\\right]\\right]\\right)\\right]$"
      ],
      "text/plain": [
       "⎡⎛       ⎡⎡-1⎤  ⎡-1⎤⎤⎞  ⎛      ⎡⎡1 ⎤⎤⎞⎤\n",
       "⎢⎜       ⎢⎢  ⎥  ⎢  ⎥⎥⎟  ⎜      ⎢⎢  ⎥⎥⎟⎥\n",
       "⎢⎜-2, 2, ⎢⎢1 ⎥, ⎢0 ⎥⎥⎟, ⎜1, 1, ⎢⎢-1⎥⎥⎟⎥\n",
       "⎢⎜       ⎢⎢  ⎥  ⎢  ⎥⎥⎟  ⎜      ⎢⎢  ⎥⎥⎟⎥\n",
       "⎣⎝       ⎣⎣0 ⎦  ⎣1 ⎦⎦⎠  ⎝      ⎣⎣1 ⎦⎦⎠⎦"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[1,3,3], [-3, -5, -3], [3,3,1]])\n",
    "eig = sy.matrices.MatrixEigen.eigenvects(A)\n",
    "eig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Construct $P$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIkAAABLCAYAAABEBKR2AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEnklEQVR4Ae1c0W3UQBD1Ib5RBBIFXAkBKiDpgNAB0EEQFaDQQUgFiJQAFURJB6EAJKKIBsKb027Oe5ojTm5uPeN9K1n2zvrsN2/e7a5vPTfrum4X2zk2rZze3NwcaA20TYeB2Wx2CW/mmkeI/+xxr+ELjuXkfvnVr/B4sgwcKZ7tw/ZG7H2RHEM1FIXC1tRNiPvXVR/Ru4hpIZJHq43bruPmu9K9YdvZ9r2sr+8Z+zax9XsSa05vr5cEcQLDFbaX2NTx7/YDjg48Y6+FrYpI0J1dI+6LCTAcO8SxTJZDFM/Ya2GrPtyEUAZBFgxQJAUdrGgMUCQaK7QVDFAkBR2saAxQJBortBUMDHq6SY9aP/HJ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NLKuB3UwkaQIbMsEJ2CMnlm0du8lwM5FelW6sYYAiWUMMzUsGKJIlFzxawwBFsoYYmpcMUCRLLni0hoH+080lHqdWTzuNPPNfdYZ1nQHE/RItc72160Qksq4i/wmvFa65aKxMz3b0P5f+AcJoSZVGzLJKAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-1 & -1 & 1\\\\1 & 0 & -1\\\\0 & 1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-1  -1  1 ⎤\n",
       "⎢          ⎥\n",
       "⎢1   0   -1⎥\n",
       "⎢          ⎥\n",
       "⎣0   1   1 ⎦"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P = sy.zeros(3, 3)\n",
    "P[:, 0] = eig[0][2][0]\n",
    "P[:, 1] = eig[0][2][1]\n",
    "P[:, 2] = eig[1][2][0]\n",
    "P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Construct $D$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHkAAABLCAYAAABUSCjvAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGH0lEQVR4Ae1d7W0cNxS8C/Q7ECIgBZw7kOMKcu7ATgdJSghSQeB0YKeCwCrBqcCwOpALCBBbSAOXmQOfQq14Z97uLN9bmwSo3eVy+YYzy4/jPZ7Wq9XqEvEdYilc7Xa756UbPS0OA+v1+gZoNiVE0G99lt34HefMnIf3+UU/D8vAiwKyp0h7xvRc5JdQvYtaYCt6EnR7NcSI1s2kByIP80mvYZTdyS+p0O9w/MBrALxOaa4H4LPW8A+APEJ8EeWlV2DjmLxD3KBSqzkiy0Z8nZeNa5JKu9s83eMcGDgneWa2cX6OuB/nLM3rOBYbnvsJEbB3q69w0iJQ0B9zQzDOVn2L+DpPb32OVkIyzoH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Umj61Df64BA13kBiR7m+gieyvQwH4XuQHJ3ia6yN4KNLDfRW5AsreJLrK3Ag3sd5EbkOxtoovsrUAD+13kBiR7m5Ata7Ii+LZmsc710bGnb8K4jv44LXVWvzsykQGCzvW/AcD+x8FxTYeBdzg+RZqb00ANE1GxJw7/QB3oJcKdG5ua+gzzSLprgFmsc31k7GyxiPyNUnqi/DkUr/ZaIjKMSZzAa0GL8y0ZexUVKpG3sFZyPLtNKHg/algy9ipOJ4ucxo1PGTOvzU/la3p/ydhPIWqyyDBmAlqrze1b6zavzfxehPMlY6/mTyFyjbGLmkxB8ywZ+55ShcjWWksaWUtxc64vgcrSlow9q8bx08kic5qfTJS6ZEsL+Tl5ydiPy3r/7mSRU3FTnMDvI2p/tWTsVWypRJY5gVeh1mZaMvYqJiQio9vjv3b9gI8k3EqyD+njyQ+4uPc/GtPtMIclY68lUbZ2DYOTncBrQc+QLyx2NBb2NJzbcO2agd8HXOP4Fi9o1f/UkomcJjGLdK6PjB3YDv3SwV7xmj+S7rrGUM/jx0AX2Y/7Zpa7yM2o9jPURfbjvpnlLnIzqv0M5bPrG0zNh0iuFLO7YaH9WssAdOMm+c2hUiky15UP/dBJyDXnQ5X5gtPNgbJIwX/zfMRsRuzRJwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 0 & 0\\\\0 & -2 & 0\\\\0 & 0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-2  0   0⎤\n",
       "⎢         ⎥\n",
       "⎢0   -2  0⎥\n",
       "⎢         ⎥\n",
       "⎣0   0   1⎦"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = sy.diag(eig[0][0], eig[0][0], eig[1][0])\n",
    "D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can verify if $PDP^{-1}=A$ holds:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "P * D * P.inv() == A "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of course we don't need to go through this process seperately. There is ```diagonalize``` method in SymPy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "P, D = A.diagonalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIkAAABLCAYAAABEBKR2AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEnklEQVR4Ae1c0W3UQBD1Ib5RBBIFXAkBKiDpgNAB0EEQFaDQQUgFiJQAFURJB6EAJKKIBsKb027Oe5ojTm5uPeN9K1n2zvrsN2/e7a5vPTfrum4X2zk2rZze3NwcaA20TYeB2Wx2CW/mmkeI/+xxr+ELjuXkfvnVr/B4sgwcKZ7tw/ZG7H2RHEM1FIXC1tRNiPvXVR/Ru4hpIZJHq43bruPmu9K9YdvZ9r2sr+8Z+zax9XsSa05vr5cEcQLDFbaX2NTx7/YDjg48Y6+FrYpI0J1dI+6LCTAcO8SxTJZDFM/Ya2GrPtyEUAZBFgxQJAUdrGgMUCQaK7QVDFAkBR2saAxQJBortBUMDHq6SY9aP/HJ+/y2cYDZ90VxtxEqkbGPQJd6y0EiSY9aL9QrODdGxu6FWg43XiLhGAdF4jg4XqBRJF4i4RjHoDmJBX5MIL/jOjLxlbUbKeewycT2DPMGeU3BbfGMvQa2aiKBEMK+vOQZew1sHG7c9l9+gFEkfmLhFglF4jY0foBRJH5i4RYJReI2NH6AUSR+YuEWCUXiNjR+gFEkfmLhFglF4jY0foBRJH5i4RaJuUiwlhAy+Qq4j9J2iP0xtrnbqD0AGPx5cFxM1m4AQBbuQiZfCd/ALwnzn7EOcprq4o8sQO7DFjb11SouJj0JiLyWhSZsH0DuNyE6SgGR74F1B9gXAhHc4g92Uj+WetQifmDbOC4mIolKYsItq9Pau7hnsO+lb2NwFzeDT5FACKBQcpRXi/QmUqS96dK0SAb2Ek+bVgicb1ok8D8LIPcafT3k3kUmsU2X1kUyJPjPhpw05XNMHoEDE5R7C82F3Mv80Rpr2NJwOHpSnIjkSXI472v47+Ie8oiIQAgWbUjJttF+J0mP4mMlxT3PQZLh5m+q5H1ua2X/A47OFWdzTyLtLZbf2WnOSbpOUj1ymkfmRfbyDb5I3+a+vbnj5kUCEcg/D15h2Fn806AoIM0F3uLwndRbL2YTVxAbNvkKIpBe4xN8yMPOK9RfQ0AX0QViERczkYDQyMlX8jvJx+iC0PBbxKX54UYjlraSAYqk5IM1hQGKRCGFppIBiqTkgzWFAYpEIYWmkgGKpOSDNYUBikQhhaaSAYqk5IM1hQGKRCGFppIBiqTkgzWFAVORYJ0gdIIT8D84gUnhtqppm9jN1m4AMmSCE3DLy0UhE8tqYTfpSQA2bIITFsBMEpiqdhvpZrWwm4gEmJngNIZKKt3TSiR7wKu9VCxL8FKknSUoAxuLJI2Ld7mf3xe96zy2O2RgY5HApyyA3Gv03cy9i0wOWYIyYCGSIa43n+A0hCSv51iIJPcWmo+5lxktwUkDRdv9GNhYJPIYlm6pDSnZNlqC0/3o4NkaAxuLJF2UCU4auxOxWYmECU4TEYTmholIMOQwwUljdyI2s7Ub8BE2wQm/9YRNLKuB3UwkaQIbMsEJ2CMnlm0du8lwM5FelW6sYYAiWUMMzUsGKJIlFzxawwBFsoYYmpcMUCRLLni0hoH+080lHqdWTzuNPPNfdYZ1nQHE/RItc72160Qksq4i/wmvFa65aKxMz3b0P5f+AcJoSZVGzLJKAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-1 & -1 & 1\\\\1 & 0 & -1\\\\0 & 1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-1  -1  1 ⎤\n",
       "⎢          ⎥\n",
       "⎢1   0   -1⎥\n",
       "⎢          ⎥\n",
       "⎣0   1   1 ⎦"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHkAAABLCAYAAABUSCjvAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGH0lEQVR4Ae1d7W0cNxS8C/Q7ECIgBZw7kOMKcu7ATgdJSghSQeB0YKeCwCrBqcCwOpALCBBbSAOXmQOfQq14Z97uLN9bmwSo3eVy+YYzy4/jPZ7Wq9XqEvEdYilc7Xa756UbPS0OA+v1+gZoNiVE0G99lt34HefMnIf3+UU/D8vAiwKyp0h7xvRc5JdQvYtaYCt6EnR7NcSI1s2kByIP80mvYZTdyS+p0O9w/MBrALxOaa4H4LPW8A+APEJ8EeWlV2DjmLxD3KBSqzkiy0Z8nZeNa5JKu9s83eMcGDgneWa2cX6OuB/nLM3rOBYbnvsJEbB3q69w0iJQ0B9zQzDOVn2L+DpPb32OVkIyzoHnymzjnLh4/dLSPI4qbK1E3oKkjwDNFpKHN7g4Rzpbulfgp4fSkPEW6dsC5pY4JdhaiUwx36cWUiJpKH4pz1xpfAE5PxgGtmYG3vcKEmxnLdBD3EOftTkf4MBRakmzQ6tspd/MDqRgQImtVUt+UA1UggKzm7YZ94M8DRJMQGu1uUlr3V69jAybm8hgkxOuV2jFXISJHC4Cg6vCVtVdp67jL1T2lLf6+aFuGOVx1voG9392JtBaawmGtSR+bvYIMmxVIkMMdmePFTWFwPzIwnHYW2BiuAUewim9vJbmsgqoxNa0uwahXGZ7lAuMtA0jmXYKnPmX7FtL5n2vIMHWTGQIeQmmnkDg4USLwh/rmuYmmHMDLrMOA3uua7ao4Y2G1zJsJL/FsiaXCTkW55GVuAGR7DrdIjEgDpc1PyLt0hMXbY/FhufuljWrxmRamhgoJrvE/Xg8KOt6cO1xyVb7azZsPMH19yD5s8DWRGSQJZm0zaU+8LFLHg4jc5k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HXOP4Fi9o1f/UkomcJjGLdK6PjB3YDv3SwV7xmj+S7rrGUM/jx0AX2Y/7Zpa7yM2o9jPURfbjvpnlLnIzqv0M5bPrG0zNh0iuFLO7YaH9WssAdOMm+c2hUiky15UP/dBJyDXnQ5X5gtPNgbJIwX/zfMRsRuzRJwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 0 & 0\\\\0 & -2 & 0\\\\0 & 0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-2  0   0⎤\n",
       "⎢         ⎥\n",
       "⎢0   -2  0⎥\n",
       "⎢         ⎥\n",
       "⎣0   0   1⎦"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes we just want to test if a matrix is diagonalizable, then use ```is_diagonalizable``` in SymPy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.is_diagonalizable()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
